
Page({

  data: {
    id: '',
    Tinglitxt: '',
    isShow: false,
    name: '',
    author: '爱伦．坡',
    src: '',
    img: 'https://gss2.bdstatic.com/-fo3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=177e8ebe0dfa513d51aa6bd8055632c6/314e251f95cad1c82177e7457a3e6709c83d5187.jpg'
  },

  onReady() {
    this.audioCtx = wx.createAudioContext('myAudio');
  },

  onLoad: function (options) {
    const { id } = options;
    this.setData({
      id,
      name: '莫尔格街凶杀案' + id,
      src: 'http://localhost/' + id + '.mp3'
    }, () => {
      this.getTinglitxt();
    });
    wx.setNavigationBarTitle({
      title: '莫尔格街凶杀案' + id
    });
  },

  audioPlay: function () {
    this.audioCtx.play();
  },

  audioPause: function () {
    this.audioCtx.pause();
  },

  getTinglitxt() {
    const { id } = this.data;
    wx.getFileSystemManager().copyFile({  //先把文件复制到可操作的文件夹
      srcPath	: `static/cma/莫尔格街凶杀案${id}.txt`, //源文件
      destPath: wx.env.USER_DATA_PATH + `/莫尔格街凶杀案${id}.txt`,	//可操作的文件夹路径
      success: res => {
        wx.getFileSystemManager().readFile({  //读取文件
          filePath: wx.env.USER_DATA_PATH + `/莫尔格街凶杀案${id}.txt`,
          encoding: 'utf-8',
          success: res => {
            this.setData({
              Tinglitxt:res.data
            });
          },
        })
      }
    });
  },

  isShow() {
    const { isShow } = this.data;
    if (isShow) {
      this.setData({
        isShow: false
      });
      return;
    }
    this.setData({
      isShow: true
    });
  }
})